﻿using ExpressesClass.Model;
using System;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;

namespace ExpressesClass.IPrints
{
    public class ZopPrint : IPrint
    {
        private Kddh kddh;
        private int printCur;
        private DateTime now;

        private int _w, _5, _8, _10, _12, _13, _15, _20, _25, _35, _45;
        public ZopPrint(Kddh kddh, int printCur, DateTime now)
        {
            this.kddh = kddh;
            this.printCur = printCur;
            this.now = now;
            _w = 290;
            _5 = 20;
            _8 = 36;
            _10 = 40;
            _12 = 48;
            _13 = 52;
            _15 = 60;
            _20 = 80;
            _25 = 100;
            _35 = 140;
            _45 = 180;
        }
        public void Print(string printer)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printer;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("ZopSize", 290, 500);
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            printDoc.Print();
        }
        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //打印时间
            PrintPrintTime(g, 5, _12, _35, _12);
            //快递类型
            PrintShipName(g, _45, _5, _35, _12);
            //大笔
            PrintSortingCode(g, _5, _15 + 5, _w, _15);
            //横向面单号
            PrintHorMailNo(g, 5, _25 + 2, _w - 15, _12);
            //竖向面单号
            PrintVerMailNo(g, _w - _15 + 2, _25 + _20 - 2, _10 + _20 + _13 + _20 - 2, _8);
            //集包地
            PrintPkgCode(g, _5, _25 + _20, _w - _15, _15);
            //收
            PrintReceive(g, 5, _25 + _20 + _10 + _5);
            //寄
            PrintSend(g, 5, _25 + _20 + _10 + _20 + 8);
            //收件人
            PrintReceiveMan(g, _8 + 5, _25 + _20 + _10 + 2, _w - _8 - _15, _5);
            //判断是否存在客户
            if ((kddh.receiveCompany ?? "") != "")
            {
                //客户
                PrintReceiveCompany(g, _8 + 5, _25 + _20 + _10 + _5 + 2, _w - _8 - _15, _10);
                //收件地址
                PrintReceiveAddress(g, _8 + 5, _25 + _20 + _10 + _10 + 4, _w - _8 - _15, _10);
            }
            else
            {
                //收件地址
                PrintReceiveAddress(g, _8 + 5, _25 + _20 + _10 + _5 + 2, _w - _8 - _15, _10);
            }
            //寄件人
            PrintSendMan(g, _8 + 5, _25 + _20 + _10 + _20 + 2, _w - _8 - _15, _8);
            //寄件地址
            PrintSendAddress(g, _8 + 5, _25 + _20 + _10 + _20 + _5 - 3, _w - _8 - _15, _13);
            //备注
            PrintRemark(g, 2, _25 + _20 + _10 + _20 + _13, _w - _15, _15);
            //位置
            PrintWz(g, 2, _25 + _20 + _10 + _20 + _13 + _13, _w - _15, _8);
            //二维码
            //PrintQRMailNo(g, _x + _53 - _18 - 8, _y + _12 + _15 + _12 + _10 + _20 - 8, _18 + 15, _18 + 15);
            //签名栏
            PrintSign(g, 2, _25 + _20 + _10 + _20 + _13 + _20 + 2, _w, _12);
            //已检视
            PrintLook(g, 5, _25 + _20 + _10 + _20 + _13 + _20 + _12 + 3, _w, _10);
            //边框
            PrintRectangle(g);
        }
        private void PrintRectangle(Graphics g)
        {
            Pen pen = new Pen(Brushes.Black, 1);
            //横
            g.DrawLine(pen, 0, _25, _w, _25);
            g.DrawLine(pen, 0, _25 + _20, _w, _25 + _20);
            g.DrawLine(pen, 0, _25 + _20 + _10, _w - _15, _25 + _20 + _10);
            g.DrawLine(pen, 0, _25 + _20 + _10 + _20, _w - _15, _25 + _20 + _10 + _20);
            g.DrawLine(pen, 0, _25 + _20 + _10 + _20 + _13, _w - _15, _25 + _20 + _10 + _20 + _13);
            g.DrawLine(pen, 0, _25 + _20 + _10 + _20 + _13 + _20, _w, _25 + _20 + _10 + _20 + _13 + _20);
            g.DrawLine(pen, 0, _25 + _20 + _10 + _20 + _13 + _20 + _12, _w, _25 + _20 + _10 + _20 + _13 + _20 + _12);
            //竖
            g.DrawLine(pen, _8, _25 + _20 + _10, _8, _25 + _20 + _10 + _20 + _13);
            g.DrawLine(pen, _w - _15, _25 + _20, _w - _15, _25 + _20 + _10 + _20 + _13 + _20);
        }
        private void PrintPrintTime(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(now.ToString("yyyy/MM/dd HH:mm:ss"), font, Brushes.Black, rec);
        }
        private void PrintShipName(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 13, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("普通订单", font, Brushes.Black, rec);
        }
        private void PrintShipVal(Graphics g, int x, int y, float w, float h)
        {
            if (kddh.shipName == "货到付款" || kddh.shipName == "代收货款")
            {
                Font font = new Font("黑体", 6, FontStyle.Bold);
                RectangleF rec = new RectangleF(x, y, w, h);
                RectangleF rec1 = new RectangleF(x, y + 10, w, h);
                g.DrawString(kddh.shipName + ":", font, Brushes.Black, rec);
                g.DrawString("¥" + kddh.shipVal, font, Brushes.Black, rec1);
            }
        }
        private void PrintPkgCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 15, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.pkgCode, font, Brushes.Black, rec);
        }
        private void PrintHorMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = w;
            encodeOption.Height = h;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.mailNo);
            g.DrawImage(img, x, y);

            string str = kddh.mailNo.Length > 8 ? kddh.mailNo.Substring(0, 4) + "    " + kddh.mailNo.Substring(4, 4) + "    " + kddh.mailNo.Substring(8) : kddh.mailNo;
            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            RectangleF rec = new RectangleF(x + _10, y + h + 2, w, _15);
            g.DrawString(str, font, Brushes.Black, rec);
        }
        private void PrintVerMailNo(Graphics g, int x, int y, int w, int h)
        {
            Bitmap bitmap = new Bitmap(w, h + _15);

            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = w;
            encodeOption.Height = h;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.mailNo);

            Graphics tempG = Graphics.FromImage(bitmap);
            tempG.DrawImage(img, 0, 0);

            string str = kddh.mailNo.Length > 8 ? kddh.mailNo.Substring(0, 4) + "    " + kddh.mailNo.Substring(4, 4) + "    " + kddh.mailNo.Substring(8) : kddh.mailNo;
            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            RectangleF rec = new RectangleF(_10, h, w, _15);
            tempG.DrawString(str, font, Brushes.Black, rec);

            bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
            g.DrawImage(bitmap, x, y);
        }
        private void PrintSortingCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sortingCode, font, Brushes.Black, rec);
        }
        private void PrintReceive(Graphics g, int x, int y)
        {
            RectangleF rect = new RectangleF(x, y, 24, 24);
            g.FillEllipse(Brushes.Black, rect);

            Point point1 = new Point(x + 6, y + 22);
            Point point2 = new Point(x + 12, y + 29);
            Point point3 = new Point(x + 18, y + 22);
            Point[] pntArr = { point1, point2, point3 };
            g.FillPolygon(Brushes.Black, pntArr);

            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            g.DrawString("收", font, Brushes.White, x + 2, y + 2);
        }
        private void PrintSend(Graphics g, int x, int y)
        {
            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            g.DrawString("寄", font, Brushes.Black, x + 2, y + 3);
        }
        private void PrintReceiveMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveMan + " " + kddh.receiveManPhone, font, Brushes.Black, rec);
        }
        private void PrintReceiveCompany(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("客户:" + kddh.receiveCompany, font, Brushes.Black, rec);
        }
        private void PrintReceiveAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.receiveManAddress, font, Brushes.Black, rec);
        }
        private void PrintSendMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sendMan + " " + kddh.sendManPhone, font, Brushes.Black, rec);
        }
        private void PrintSendAddress(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.sendManAddress, font, Brushes.Black, rec);
        }
        private void PrintQRMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = w;
            encodeOption.Height = h;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.QR_CODE;
            encodeOption.PureBarcode = false;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.mailNo);

            g.DrawImage(img, x, y);
        }
        private void PrintSign(Graphics g, float x, float y, float w, float h)
        {
            Font font1 = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec1 = new RectangleF(x, y, w, h);
            g.DrawString("本次服务适用中通官网(www.zto.com)公示的快递服务协议条款。您对此单的签收代表您已收到快件且包装完好无损。", font1, Brushes.Black, rec1);

            Font font2 = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec2 = new RectangleF(x, y + 22, w, h);
            g.DrawString("签名栏/时间:", font2, Brushes.Black, rec2);
        }
        private void PrintRemark(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.remark;

            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("序号:" + printCur + ";" + str, font, Brushes.Black, rec);
        }
        private void PrintWz(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.wzh;

            Font font = new Font("微软雅黑", 16, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
        private void PrintLook(Graphics g, float x, float y, float w, float h)
        {
            Font font1 = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec1 = new RectangleF(x, y, w, h);
            g.DrawString("全国统一客服热线:95311", font1, Brushes.Black, rec1);

            Font font2 = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec2 = new RectangleF(x + _35 + _8, y, w, h);
            g.DrawString("已检视  已实名", font2, Brushes.Black, rec2);
        }

        #region 云打印-暂弃用
        //private static string url = ConfigurationManager.AppSettings["zop-url"].ToString();//对应的地址
        //private static string companyId = ConfigurationManager.AppSettings["zop-companyId"].ToString();//合作商编码
        //private static string key = ConfigurationManager.AppSettings["zop-key"].ToString();//合作商签名key
        //private static string partner = ConfigurationManager.AppSettings["zop-partner"].ToString();//商家ID，又称电子面单账号、电子面单账户、客户ID
        //private static string verify = ConfigurationManager.AppSettings["zop-verify"].ToString();//电子面单账号验证码，由合作网点提供
        //private static string branchId = ConfigurationManager.AppSettings["zop-branchId"].ToString();//网点编码，客户购买电子面单的网点
        //public void Print(string printer)
        //{
        //    ZopProperties properties = new ZopProperties(companyId, key);
        //    ZopClient client = new ZopClient(properties);
        //    printReq request = new printReq();
        //    request.url = url + request.appName;
        //    //寄件人信息
        //    printReqSender sender = new printReqSender();
        //    sender.name = kddh.sendMan;
        //    sender.mobile = kddh.sendManPhone;
        //    sender.phone = kddh.sendManPhone;
        //    sender.prov = kddh.sendProvince;
        //    sender.city = kddh.sendCity;
        //    sender.address = kddh.sendManAddress;
        //    sender.county = kddh.sendCounty;
        //    sender.zipcode = kddh.sendPostcode;
        //    //收件人信息
        //    printReqSender receiver = new printReqSender();
        //    receiver.name = kddh.receiveMan;
        //    receiver.mobile = kddh.receiveManPhone;
        //    receiver.phone = kddh.receiveManPhone;
        //    receiver.prov = kddh.receiveProvince;
        //    receiver.city = kddh.receiveCity;
        //    receiver.address = kddh.receiveManAddress;
        //    receiver.county = kddh.receiveCounty;
        //    receiver.zipcode = kddh.receivePostcode;
        //    //打印参数
        //    printReqParam param = new printReqParam();
        //    param.paramType = "DEFAULT_PRINT";
        //    param.mailNo = kddh.mailNo;
        //    param.elecAccount = partner;
        //    param.elecPwd = verify;
        //    //内容
        //    printReqDataDetail reqdata = new printReqDataDetail();
        //    reqdata.printParam = param;
        //    reqdata.sender = sender;
        //    reqdata.receiver = receiver;
        //    reqdata.partnerCode = "88LEdVFZbxONnD4sqIV6JQ==";
        //    reqdata.printChannel = "ZOP";
        //    reqdata.deviceId = printer;
        //    reqdata.printerId = printer;
        //    reqdata.printType = "REMOTE_EPRINT";
        //    reqdata.remark = kddh.remark;
        //    request.addParam(request.paramName, JsonParser.objToJson(reqdata));
        //    printResDataDetail response = JsonParser.jsonToObj<printResDataDetail>(client.execute(request));
        //    if (response != null && response.status)
        //    {
        //        MessageBox.Show("打印成功.", "提示");
        //    }
        //    else if (response != null)
        //    {
        //        MessageBox.Show(response.message, "提示");
        //    }
        //    else
        //    {
        //        MessageBox.Show("打印失败.", "提示");
        //    }
        //}
        #endregion

    }
}
